#pragma once
#include <memory>
#include <string>
#include <mutex>
#include "lvgl.h"
#include "json.h"
#include <ctime>
#include <chrono>
#include <iomanip>

namespace Media{
class DailySchedule{
public:
    using Ptr = std::shared_ptr<DailySchedule>;
    static Ptr Instance();
    void DailyTransactionPage();
    void Dottedline();
    void TimeHour();
    void clearResources();

    DailySchedule();
    ~DailySchedule(){}

private:
    
    
    bool getDailyScheduleDate();

    bool filterShowSchedule(Json::Value items);

    bool parseISO8601ToTimeT(const std::string& iso8601, std::time_t& timeT);

    bool sortSchedule(Json::Value &table);

    bool groupSchedule(const Json::Value table, Json::Value& groupTable);

    bool ScheduleToShowTable(Json::Value& groupTable, Json::Value& showTable);

    bool unoverlapGroup(Json::Value& groupScheduleTable, Json::Value& unoverlapTable);

    bool caculCoordinate(const Json::Value &showTable, Json::Value &coordinateTable, unsigned int width, unsigned int height,  unsigned int stepLenth);

    void showScheduleData(const Json::Value &coordinateTable);

    bool createFont(std::string path, uint16_t weight, uint16_t style, lv_ft_info_t &font);
private:
    lv_obj_t * m_dailyTransactionPage = nullptr;

    lv_obj_t * m_dailyTransactionTop = nullptr;
    lv_obj_t * m_dailyTransactionTopLabel = nullptr;
    
    lv_obj_t * m_dailyTransactionContent = nullptr;

    lv_obj_t * m_dailyTransactionMiddle = nullptr;
    lv_obj_t * m_dailyTransactionMiddleLabel = nullptr;

    lv_obj_t * m_dailyTransactionBottomSupper = nullptr;
    lv_obj_t * m_dailyTransactionBottomLeftSupper = nullptr;

    lv_obj_t * m_dailyTransactionMiddleLeftSupper = nullptr;
    lv_obj_t * m_dailyTransactionBottomRightSupper = nullptr;

    lv_obj_t * m_DotlineFrame = nullptr;
    lv_obj_t * m_DotlineItem = nullptr;
    lv_obj_t * m_TimeHourFrame = nullptr;
    lv_obj_t * m_TimeHourItem = nullptr;
    std::vector<lv_obj_t *> m_scheduleItem;
    lv_obj_t * m_scheduleLabelItem = nullptr;

    lv_style_t m_dailyTransactionPage_style;
    
    lv_style_t m_dailyTransactionTop_style;
    lv_style_t m_dailyTransactionTopLabelStyle;
    lv_style_t m_dailyTransactionTopLabelStyleDayStyle;

    lv_style_t m_dailyTransactionMiddle_style;
    lv_style_t m_LabelDayGmtStyle;
    lv_style_t m_LabelDayDayStyle;
    
    lv_style_t m_dailyTransactionBottomSupper_style;
    lv_style_t m_dailyTransactionMiddleLeftSupper_style;
    lv_style_t m_dailyTransactionMiddleMiddleSupper_style;
    lv_style_t m_dailyTransactionBottomRightSupper_style;

    lv_style_t  m_TimeHourFrameStyle;
    lv_style_t  m_TimeHourItemStyle;
    lv_style_t  m_DotlineFrameStyle;
    lv_style_t  m_DotlineItemStyle;
    
    std::vector<lv_style_t>  m_scheduleItemStyles;
    lv_style_t               m_scheduleSummaryStyle;
    lv_style_t               m_scheduleTimeStyle;
    
    

    lv_coord_t m_dotlineNum;
    lv_coord_t m_padding;



    Json::Value m_dailyTable;
    Json::Value m_items{Json::arrayValue};
    Json::Value m_group{Json::arrayValue};
    Json::Value m_showTable{Json::arrayValue};
    Json::Value m_coordinateTable{Json::arrayValue};
    
    time_t m_curTime;
    std::tm* m_curtm;
    std::string m_timeZone;
    
    int m_scheduleIndex = -1;


    lv_ft_info_t m_font32Normal;
    lv_ft_info_t m_font32Bold;
    lv_ft_info_t m_font16Normal;
    lv_ft_info_t m_font20Normal;
    lv_ft_info_t m_font20Bold;
    lv_ft_info_t m_font12Normal;
    std::mutex m_showLock;

};
}